import Vue from 'vue'
import Vuex from 'vuex'
import {userInfoAPI} from "../network/usercenter";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userLogin:false,
    userInfo:{}
  },
  getters:{
    getUserInfo:state => {
      return state.userInfo
    },
    getUserLogin:state => {
      return state.userLogin
    }
  },
  //同步函数
  //使用store.commit 方法触发
  mutations: {
    updateUserInfo(state,isLogin,user){
      state.userInfo = user
      state.userLogin = isLogin
    }

  },
  //异步函数定义在actions
  //通过store.dispatch 方法触发
  actions: {
    refreshUserInfo({commit}){
      //调用用户信息获取API
      userInfoAPI().then(res=>{
        console.log(res)
        commit('updateUserInfo',true,res)

      }).catch(error=>{
        console.log(error)
        commit('updateUserInfo',false, {})
      })

    }
  },
  modules: {
  }
})
